﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;

namespace WPFSolution.CommonOperations
{
    public class GUIManipulator
    {
        public static  double GetDoubleValue(TextBox source, double errorValue, double maxValue, double minValue)
        {
            if (source != null)
            {
                double val;
                bool parsed = Double.TryParse(source.Text, out val);
                if (!parsed || val > maxValue || val < minValue)
                {
                    source.Text = errorValue.ToString();
                    return errorValue;
                }
                return val;
            }
            return errorValue;
        }

        public static  int GetIntValue(TextBox source, int errorValue, int maxValue, int minValue)
        {
            if (source != null)
            {
                int val;
                bool parsed = int.TryParse(source.Text, out val);
                if (!parsed || val > maxValue || val < minValue)
                {
                    source.Text = errorValue.ToString();
                    return errorValue;
                }
                return val;
            }
            return errorValue;
        }

        public static void AddLineToGraph(Chart chart, IEnumerable points, string legend, Style pointStyle)
        {
            var seria =
                new LineSeries()
                {
                    ItemsSource = points,
                    DependentValuePath = "Y",
                    IndependentValuePath = "X",
                    Title = legend,
                    DataPointStyle = pointStyle
                };

            chart.Series.Add(seria);
        }
    }
}
